package com.it2297.controller;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;

import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.alee.extended.image.WebDecoratedImage;
import com.it2297.component.ImageChooser;
import com.it2297.component.ImageCropper;
import com.it2297.main.UserApp;
import com.it2297.model.Needy;
import com.it2297.model.Product;
import com.it2297.model.User;

import com.it2297.model.Workshop;
import com.it2297.util.ImageProcessor;
import com.it2297.view.CreateWorkshopView;
import com.it2297.view.DeleteWorkshopView;
import com.it2297.view.NeedyView;
import com.it2297.view.UpdateWorkshopView;
import com.it2297.view.WorkshopSearchView;
import com.it2297.view.WorkshopView;

public class WorkshopController {
	private CreateWorkshopView view;
	private UpdateWorkshopView updatev;
	private DeleteWorkshopView deletev;
	private BufferedImage workshopimg;
	private WorkshopSearchView search;
	private ArrayList<Workshop> workshopdisplayList = new ArrayList<Workshop>();
	private WorkshopView wsview;
	
	public WorkshopController(CreateWorkshopView nv){
		this.view = nv;
	}
	public WorkshopController(UpdateWorkshopView v){
		this.updatev=v;
	}
	public WorkshopController(DeleteWorkshopView s){
		this.deletev=s;
	}
	public WorkshopController(WorkshopSearchView sr){
		this.search=sr;
	}
	public WorkshopController(WorkshopView wsv){
		this.wsview=wsv;
	}
	public void CreateWorkshop() {
		
		Workshop workshop=new Workshop();		
		workshop.setWorkshopId(view.getWorkshopId());
		workshop.setCapacity(view.getCapacity());
		workshop.setLocation(view.getlocation());
		workshop.setStatus(view.getStatus());
		workshop.setTitle(view.getTitle());
		workshop.setAvailableSlot(view.getAvailableSlot());
		workshop.setDescription(view.getDescription());
		workshop.setDateStart(view.getStartDate());
		workshop.setDateEnd(view.getEndDate());
		workshop.setNric(view.getNRIC());
		
		if(!Workshop.IsWorkshopIdExists(view.getWorkshopId())){
		workshop.createWorkshop(workshop);
		JOptionPane.showMessageDialog(null, "Workshop records created successfully!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Duplicate workshop id!");
		}
		
		
	}
	
	public void UpdateWorkshop(){
		Workshop workshop=new Workshop();
		
		workshop.setWorkshopId(updatev.getWorkshopId());
		workshop.setCapacity(updatev.getCapacity());
		workshop.setLocation(updatev.getlocation());
		workshop.setStatus(updatev.getStatus());
		workshop.setTitle(updatev.getTitle());
		workshop.setAvailableSlot(updatev.getAvailableSlot());
		workshop.setDescription(updatev.getDescription());
		workshop.setDateStart(updatev.getStartDate());
		workshop.setDateEnd(updatev.getEndDate());
		workshop.setNric(updatev.getNRIC());
		workshop.updateWorkshop(workshop);	
		
		}
	public void getSearch(String title){
		Workshop workshop=new Workshop();
		workshop.getSingleSearch(title);
		
		search.setWorkshopId(workshop.getWorkshopId());
		search.setStartDate(workshop.getDateStart());
		search.setEndDate(workshop.getDateEnd());
		search.setDescription(workshop.getDescription());
		search.setlocation(workshop.getLocation());
		search.setCapacity(workshop.getCapacity());
		search.setAvailableSlot(workshop.getAvailableSlot());
		search.setNRIC(workshop.getNric());
	}
		
		
	public void getWorkshop(int id){		
		Workshop workshop=new Workshop();
		workshop.getSingle(id);
		
		updatev.setStatus(workshop.getStatus());
		updatev.setTitle(workshop.getTitle());
		updatev.setStartDate(workshop.getDateStart());
		updatev.setEndDate(workshop.getDateEnd());
		updatev.setDescription(workshop.getDescription());
		updatev.setlocation(workshop.getLocation());
		updatev.setCapacity(workshop.getCapacity());
		updatev.setAvailableSlot(workshop.getAvailableSlot());
		updatev.setStatus(workshop.getStatus());
		updatev.setNRIC(workshop.getNric());
	
	}
	public void getWorkshopDetail(int id){
		Workshop workshop=new Workshop();
		workshop.getSingle(id);
		
		deletev.setStatus(workshop.getStatus());
		deletev.setTitle(workshop.getTitle());
		deletev.setStartDate(workshop.getDateStart());
		deletev.setEndDate(workshop.getDateEnd());
		deletev.setDescription(workshop.getDescription());
		deletev.setlocation(workshop.getLocation());
		deletev.setCapacity(workshop.getCapacity());
		deletev.setAvailableSlot(workshop.getAvailableSlot());
		deletev.setStatus(workshop.getStatus());
		deletev.setNRIC(workshop.getNric());
	}
	public void WorkshopStatusDel() {
		Workshop workshop=new Workshop();
		workshop.setWorkshopId(deletev.getWorkshopId());
		workshop.delWorkshop(workshop);
	}
	public ImageIcon displayimageChooser() {
		ImageChooser imgChooser = new ImageChooser();
		imgChooser.setMultiSelectionEnabled(false);
		imgChooser.showDialog();
		ImageIcon icon = null;
		workshopimg = imgChooser.getSelectedImage();

		if (workshopimg != null) {
			workshopimg = ImageProcessor.resize(workshopimg, 160, 120, true, true);
			icon = new ImageIcon(workshopimg);

			WebDecoratedImage wdimg = new WebDecoratedImage(icon);
			wdimg.addMouseListener(new MouseAdapter() {

				
				
			});

		} else {
			System.out.println("No Image");
		}

		return icon;
	}

	public void saveImages() {
		try {
			if (workshopimg == null) {

			} else {
				ImageIO.write(workshopimg, "jpg", new File("img/Workshopimg/"
						+ view.getWorkshopId() + ".jpg"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public int getWorkshopRecordNumber() {
		return Workshop.getWorkshopRecordNumber();
	}
	public ArrayList<Workshop> getMultipleWorkshop(int index, int count) {
		return Workshop.getMultipleWorkshops(index, count);
	}
	
	
		
		
	}
	
		
